package com.hrbxlh.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.DepActionImpl;
import com.hrbxlh.actionImpl.MemberActionImpl;
import com.hrbxlh.actionImpl.MenuActionImpl;
import com.hrbxlh.actionImpl.RoleActionImpl;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：MenuAction.java
* Description：菜单action类(暂时不做，没用上)
* History：
* Date           Author               Desc
* 2017.1.9		  xzx                 建立
* -------------------------------------------------------
*/

@Controller
public class MenuAction {

	@Autowired
	@Qualifier("memberActionImpl")
	private MemberActionImpl memberActionImpl;
	
	@Autowired
	@Qualifier("depActionImpl")
	private DepActionImpl depActionImpl;
	
	@Autowired
	@Qualifier("roleActionImpl")
	private RoleActionImpl roleActionImpl;
	
	@Autowired
	@Qualifier("menuActionImpl")
	private MenuActionImpl menuActionImpl;
	
	/**
	 * @param request
	 * @param response
	 * @return  跳转到添加菜单页面
	 * @throws IOException
	 */
	@RequestMapping(value = "/skipAddMenu", method = RequestMethod.GET)
	public ModelAndView skipAddMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MenuAction.class.getName());
		try {
			//获取全部菜单
			List<Map<String, Object>> menuList = menuActionImpl.getMenuList(returnMap);
			resultMap.put("menuList", menuList);
			//添加菜单页面名称
			pageName = "pc/addMenu";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转添加菜单页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}

	/**
	 * @param request
	 * @param response
	 * @return  添加菜单,传回菜单名称，父级菜单id。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addMenu", method = RequestMethod.POST)
	public void addMenu(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MenuAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			/*模拟参数开始*/
			//Map<String, Object> mapUse=new HashMap<String, Object>();
			//mapUse.put("menuName", "测试菜单999");
			//mapUse.put("parentId", 1);
			//jsonStr = mapper.writeValueAsString(mapUse);
			/*模拟参数结束*/
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//添加菜单
			boolean flag = menuActionImpl.addMenu(paramMap);
			//返回参数
			resultMark = (flag == true) ? "success" : "fail";
			resultMap.put("result", resultMark);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加菜单出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  进修改菜单页面,参数：菜单id，传回该菜单信息，以及全部菜单信息。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipSaveMenu", method = RequestMethod.GET)
	public ModelAndView skipSaveMenu(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MenuAction.class.getName());
		try {
			/*模拟参数开始*/
			//Map<String, Object> mapUse = new HashMap<String, Object>();
			//mapUse.put("MenuId", 1);
			//jsonStr = mapper.writeValueAsString(mapUse);
			/*模拟参数结束*/
			
			//获取页面参数值
			Map<String,Object> preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取该菜单信息
			List<Map<String, Object>> menuMessageList = menuActionImpl.getMenuList(preParamMap);
			if(menuMessageList != null && menuMessageList.size() != 0){
				resultMap.put("menu", menuMessageList.get(0));
			}
			//获取全部菜单信息
			List<Map<String, Object>> menuList = menuActionImpl.getMenuList(resultMap);
			//返回值
			resultMap.put("menuList", menuList);
		    //修改菜单页面名称
			pageName = "pc/saveMenu";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转修改菜单页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @param request
	 * @param response  
	 * @return  修改菜单,传回菜单id，菜单名称，父菜单id，菜单链接  
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveMenu", method = RequestMethod.POST)
	public void saveMenu(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MenuAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			/*模拟参数开始*/
			//Map<String, Object> mapUse = new HashMap<String, Object>();
			//mapUse.put("menuId", 4);
			//mapUse.put("menuName", "FFFF");
			//mapUse.put("parentId", 1);
			//jsonStr = mapper.writeValueAsString(mapUse);
			/*模拟参数结束*/
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //修改菜单
			boolean flag = menuActionImpl.saveMenu(paramMap);
			resultMark = (flag == true) ? "success" : "fail";
			resultMap.put("result", resultMark);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("修改菜单出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**  
	 * @param request
	 * @param response
	 * @return  删除菜单,参数：菜单id集合。List<int>形式   jsonStr = [1,2,3...]
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delMenus", method = RequestMethod.POST)
	public void delMenus(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MenuAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try {
			/*模拟参数开始*/
			//List<Integer> listUse = new ArrayList<Integer>();
			
			//listUse.add(2);
			//listUse.add(3);
			//jsonStr = mapper.writeValueAsString(listUse);
			/*模拟参数结束*/
			
			//获取参数值
			List<String> paramList = mapper.readValue(jsonStr, list.getClass());
            //删除菜单操作
			Map<String, String> delResult = menuActionImpl.delMenu(paramList);
			//返回成功失败
			resultMark = (delResult.get("result").equals("true")) ? "success" : "fail";
			resultMap.put("result", resultMark);
			
			if(resultMark.equals("fail")){
				resultMap.put("errorMsg", delResult.get("errorMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除菜单出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的：" + reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @param pageNow,pageSize(一般是10)，页数从1开始，第一页pageNow传1
	 * @return  菜单列表页面，传回全部菜单信息。进页面，翻页，条件查询都是这个。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getMenuList", method = RequestMethod.GET)
	public ModelAndView getMenuList(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MenuAction.class.getName());
		try {
			/*模拟参数开始*/
			//Map<String, Object> mapUse = new HashMap<String, Object>();
			//mapUse.put("pageNow", 1);
			//mapUse.put("pageSize", 10);
			//mapUse.put("menuName", "啦啦啦");
			//jsonStr = mapper.writeValueAsString(mapUse);
			/*模拟参数结束*/
			
			//获取页面参数值
			Map<String, Object> preParamMap = mapper.readValue(jsonStr, returnMap.getClass());
			//获取菜单列表信息
			Map<String, Object> resultAllMap = menuActionImpl.getMenuListByPage(preParamMap);
			//返回值
			resultMap.put("menuList", resultAllMap.get("menuList"));
			resultMap.put("totalPage", resultAllMap.get("totalPage"));
			resultMap.put("pageNow", resultAllMap.get("pageNow"));
			resultMap.put("pageSize", preParamMap.get("pageSize"));
			resultMap.put("menuName", preParamMap.get("menuName"));
			resultMap.put("menuId", preParamMap.get("menuId"));
		    //菜单列表页面名称
			pageName = "pc/menuList";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("菜单列表页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
}